/*
 * Copyright (c) 2007 Alexey Vatchenko <av@bsdua.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>
#include <errno.h>
#include <string.h>

#include "defs.h"

/*
 * Implementation of Mac Cyrillic (version c03). Table information is taken from
 * http://www.unicode.org/Public/MAPPINGS/VENDORS/APPLE/CYRILLIC.TXT
 */

static const u_short mac_cyrillic2wc[128] = {
	0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417,
	0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F,
	0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427,
	0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F,
	0x2020, 0x00B0, 0x0490, 0x00A3, 0x00A7, 0x2022, 0x00B6, 0x0406,
	0x00AE, 0x00A9, 0x2122, 0x0402, 0x0452, 0x2260, 0x0403, 0x0453,
	0x221E, 0x00B1, 0x2264, 0x2265, 0x0456, 0x00B5, 0x0491, 0x0408,
	0x0404, 0x0454, 0x0407, 0x0457, 0x0409, 0x0459, 0x040A, 0x045A,
	0x0458, 0x0405, 0x00AC, 0x221A, 0x0192, 0x2248, 0x2206, 0x00AB,
	0x00BB, 0x2026, 0x00A0, 0x040B, 0x045B, 0x040C, 0x045C, 0x0455,
	0x2013, 0x2014, 0x201C, 0x201D, 0x2018, 0x2019, 0x00F7, 0x201E,
	0x040E, 0x045E, 0x040F, 0x045F, 0x2116, 0x0401, 0x0451, 0x044F,
	0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437,
	0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,
	0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447,
	0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x20AC
};

static const u_short wc2mac_cyrillic_00[] = {
	0xca, 0x00, 0x00, 0xa3, 0x00, 0x00, 0x00, 0xa4,
	0x00, 0xa9, 0x00, 0xc7, 0xc2, 0x00, 0xa8, 0x00,
	0xa1, 0xb1, 0x00, 0x00, 0x00, 0xb5, 0xa6, 0x00,
	0x00, 0x00, 0x00, 0xc8,
};

static const u_short wc2mac_cyrillic_04[] = {
	0xdd, 0xab, 0xae, 0xb8, 0xc1, 0xa7, 0xba, 0xb7,
	0xbc, 0xbe, 0xcb, 0xcd, 0x00, 0xd8, 0xda, 0x80,
	0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88,
	0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90,
	0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
	0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xe0,
	0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8,
	0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0,
	0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
	0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, 0x00,
	0xde, 0xac, 0xaf, 0xb9, 0xcf, 0xb4, 0xbb, 0xc0,
	0xbd, 0xbf, 0xcc, 0xce, 0x00, 0xd9, 0xdb,
};

static const u_short wc2mac_cyrillic_20[] = {
	0xd0, 0xd1, 0x00, 0x00, 0x00, 0xd4, 0xd5, 0x00,
	0x00, 0xd2, 0xd3, 0xd7, 0x00, 0xa0, 0x00, 0xa5,
	0x00, 0x00, 0x00, 0xc9, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0xff,
};

static const u_short wc2mac_cyrillic_22[] = {
	0xc6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0xc3, 0x00, 0x00, 0x00,
	0xb0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0xc5, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0xad, 0x00, 0x00, 0x00, 0xb2, 0xb3,
};

int	mac_cyrillic_mbtowc(struct conv_state *cs, const char *in,
	    size_t insize, WIDECHAR *out, size_t *inused);
int	mac_cyrillic_wctomb(struct conv_state *cs, WIDECHAR in, char *out,
	    size_t outsize, size_t *outused);

/* ARGSUSED */
int
mac_cyrillic_mbtowc(struct conv_state *cs, const char *in, size_t insize,
    WIDECHAR *out, size_t *inused)
	{
		u_char ch;

	ch = *(u_char *)in;
	*out = (ch < 0x80) ? ch : mac_cyrillic2wc[ch - 0x80];
	*inused = 1;
	return (0);
}

int
mac_cyrillic_wctomb(struct conv_state *cs, WIDECHAR in, char *out,
    size_t outsize, size_t *outused)
{
	u_char ch;

	if (outsize < 1)
		return (E2BIG);

	if (in < 0x80)
		*out = (char)in;
	else {
		ch = 0;
		if (in >= 0x00a0 && in < 0x00a0 + sizeof(wc2mac_cyrillic_00))
			ch = wc2mac_cyrillic_00[in - 0x00a0];
		else if (in == 0x00f7)	/* #DIVISION SIGN */
			ch = 0xd6;
		else if (in == 0x0192)	/* #LATIN SMALL LETTER F WITH HOOK */
			ch = 0xc4;
		else if (in >= 0x0401 && in < 0x0401 +
		    sizeof(wc2mac_cyrillic_04))
			ch = wc2mac_cyrillic_04[in - 0x0401];
		/* #CYRILLIC CAPITAL LETTER GHE WITH UPTURN */
		else if (in == 0x0490)
			ch = 0xa2;
		/* #CYRILLIC SMALL LETTER GHE WITH UPTURN */
		else if (in == 0x0491)
			ch = 0xb6;
		else if (in >= 0x2013 && in < 0x2013 +
		    sizeof(wc2mac_cyrillic_20))
			ch = wc2mac_cyrillic_20[in - 0x2013];
		/* #NUMERO SIGN */
		else if (in == 0x2116)
			ch = 0xdc;
		/* #TRADE MARK SIGN */
		else if (in == 0x2122)
			ch = 0xaa;
		else if (in >= 0x2206 && in < 0x2206 +
		    sizeof(wc2mac_cyrillic_22))
			ch = wc2mac_cyrillic_22[in - 0x2206];

		if (ch == 0)
			return (EILSEQ);
		*(u_char *)out = ch;
	}
	*outused = 1;
	return (0);
}
